package quasar;

import java.util.ArrayList;

public class Statistics
{
	// Publication statistics
	
	public static Integer subscribePacketTraffic = 0;
	// Number of PUBLISH packets originated from the publishers
	public static Integer sentPublications = 0;
	
	// Total traffic the PUBLISH packets have caused
	public static Integer publishPacketTraffic = 0;
	
	// Number of PUBLISH packets received by the end user
	public static Integer deliveredPublishPackets = 0;
	
	// Number of expected PUBLISH packets to be retrieved by the end user
	public static Integer expectedPublishPacketsRecievers = 0;
		
	// Number of subscribers for each publisher in the system
	public static ArrayList<Integer> publisherSubscribersNum = new ArrayList<Integer>();
	
	public static void init(int numStations)
	{
		// Each station is a potential publisher
		for (int index = 0 ; index < numStations ; index++)
		{
			publisherSubscribersNum.add(0);
		}
	}
	
	// Updating the number of subscribers for a specific publisher
	public static void addSubcription(int publisherID)
	{
		synchronized(publisherSubscribersNum)
		{
			publisherSubscribersNum.set(publisherID,  publisherSubscribersNum.get(publisherID) + 1);
		}
	}
	
	// Updating the expected to receive the published packets according to the number of subscribers
	public static void addPublication(int publisherID)
	{
		synchronized(expectedPublishPacketsRecievers)
		{
			expectedPublishPacketsRecievers += publisherSubscribersNum.get(publisherID);
		}
	}
	
	public static void cleanStatistics()
	{
		sentPublications = 0;
		publishPacketTraffic = 0;
		deliveredPublishPackets = 0;
		expectedPublishPacketsRecievers = 0;
		subscribePacketTraffic = 0;
		publisherSubscribersNum = new ArrayList<Integer>();
	}
}
